package controllers

import (
	"fmt"
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	"github.com/goravel/framework/support/carbon"
	httpfacade "github.com/hulutech-web/http_result"
)

type DashboardController struct {
	//Dependent services
}

func NewDashboardController() *DashboardController {
	return &DashboardController{
		//Inject services
	}
}

// Home 统计
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         DashboardController
// @Accept       json
// @Produce      json
// @Id DashboardHome
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param searchData body interface{} true "查询数据"
// @Success 200 {string} json {}
// @Router       /api/admin/dashboard/home [post]
func (r *DashboardController) Home(ctx http.Context) http.Response {
	period := ctx.Request().Input("period")
	user_id := ctx.Request().InputInt("user_id")
	start_at := ctx.Request().Input("start_at")
	end_at := ctx.Request().Input("end_at")
	db := facades.Orm().Query()
	currentDay := carbon.NewDateTime(carbon.Now()).Format("Y-m-d")
	type ChartData struct {
		Categories string  `json:"categories" form:"categories"`
		Data       float64 `json:"data" form:"data"`
	}
	var charDatas []ChartData
	switch period {
	case "day":
		if user_id == 0 {
			execSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%H') as categories,sum(score) as data from records where created_at like '%s%%' group by DATE_FORMAT(created_at,'%%H')", currentDay)
			db.Raw(execSql).Scan(&charDatas)
		} else {
			userexecSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%H') as categories,sum(score) as data from records where user_id=%d and  created_at like '%s%%' group by DATE_FORMAT(created_at,'%%H')", user_id, currentDay)
			db.Raw(userexecSql).Scan(&charDatas)
		}
	case "week":

		startOfWeek := carbon.NewDateTime(carbon.Now()).StartOfWeek().Format("Y-m-d H:i:s")
		endOfWeek := carbon.NewDateTime(carbon.Now()).EndOfWeek().Format("Y-m-d H:i:s")
		if user_id == 0 {
			execSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%Y-%%m-%%d') "+
				"as categories,sum(score) as data from records where created_at between '%s' and '%s' group by DATE_FORMAT(created_at,'%%Y-%%m-%%d')", startOfWeek, endOfWeek)
			db.Raw(execSql).Scan(&charDatas)
		} else {
			userexecSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%Y-%%m-%%d') "+
				"as categories,sum(score) as data from records where user_id=%d and created_at between '%s' and '%s' group by DATE_FORMAT(created_at,'%%Y-%%m-%%d')", user_id, startOfWeek, endOfWeek)
			db.Raw(userexecSql).Scan(&charDatas)
		}
	case "month":
		if user_id == 0 {

			startOfMonth := carbon.NewDateTime(carbon.Now()).StartOfMonth().Format("Y-m-d H:i:s")
			endOfMonth := carbon.NewDateTime(carbon.Now()).EndOfMonth().Format("Y-m-d H:i:s")
			execSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%Y-%%m-%%d') "+
				"as categories,sum(score) as data from records where created_at between '%s' and '%s' group by DATE_FORMAT(created_at,'%%Y-%%m-%%d')", startOfMonth, endOfMonth)
			db.Raw(execSql).Scan(&charDatas)
		} else {
			startOfMonth := carbon.NewDateTime(carbon.Now()).StartOfMonth().Format("Y-m-d H:i:s")
			endOfMonth := carbon.NewDateTime(carbon.Now()).EndOfMonth().Format("Y-m-d H:i:s")
			userexecSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%Y-%%m-%%d') "+
				"as categories,sum(score) as data from records where user_id=%d and created_at between '%s' and '%s' group by DATE_FORMAT(created_at,'%%Y-%%m-%%d')", user_id, startOfMonth, endOfMonth)
			db.Raw(userexecSql).Scan(&charDatas)
		}
	case "year":
		if user_id == 0 {

			startOfYear := carbon.NewDateTime(carbon.Now()).StartOfYear().Format("Y-m-d H:i:s")
			endOfYear := carbon.NewDateTime(carbon.Now()).EndOfYear().Format("Y-m-d H:i:s")
			execSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%Y-%%m') "+
				"as categories,sum(score) as data from records where created_at between '%s' and '%s' group by DATE_FORMAT(created_at,'%%Y-%%m') ", startOfYear, endOfYear)
			db.Raw(execSql).Scan(&charDatas)
		} else {
			startOfYear := carbon.NewDateTime(carbon.Now()).StartOfYear().Format("Y-m-d H:i:s")
			endOfYear := carbon.NewDateTime(carbon.Now()).EndOfYear().Format("Y-m-d H:i:s")
			userexecSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%Y-%%m') "+
				"as categories,sum(score) as data from records where user_id=%d and created_at between '%s' and '%s' group by DATE_FORMAT(created_at,'%%Y-%%m') ", user_id, startOfYear, endOfYear)
			db.Raw(userexecSql).Scan(&charDatas)
		}
	case "custom":
		if user_id == 0 {
			execSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%Y-%%m-%%d') "+
				"as categories,sum(score) as data from records where created_at between '%s' and '%s' group by DATE_FORMAT(created_at,'%%Y-%%m-%%d')", start_at, end_at)
			db.Raw(execSql).Scan(&charDatas)
		} else {
			userexecSql := fmt.Sprintf("select DATE_FORMAT(created_at,'%%Y-%%m-%%d') "+
				"as categories,sum(score) as data from records where user_id=%d and created_at between '%s' and '%s' group by DATE_FORMAT(created_at,'%%Y-%%m-%%d')", user_id, start_at, end_at)
			db.Raw(userexecSql).Scan(&charDatas)
		}
	}
	return httpfacade.NewResult(ctx).Success("", charDatas)
}

// Employee 员工工时统计
// @Summary      用户分页查询
// @Description  用户分页查询
// @Tags         DashboardController
// @Accept       json
// @Produce      json
// @Id DashboardEmployee
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param searchData body interface{} true "查询数据"
// @Success 200 {string} json {}
// @Router       /api/admin/dashboard/employee [post]
func (r *DashboardController) Employee(ctx http.Context) http.Response {
	period := ctx.Request().Input("period")
	user_id := ctx.Request().InputInt("user_id")
	start_at := ctx.Request().Input("start_at")
	end_at := ctx.Request().Input("end_at")
	db := facades.Orm().Query()
	currentDay := carbon.NewDateTime(carbon.Now()).Format("Y-m-d")
	type ShowData struct {
		UserID uint    `json:"user_id" form:"user_id"`
		Name   string  `json:"name" form:"name"`
		Data   float64 `json:"data" form:"data"`
	}
	var showDatas []ShowData
	switch period {
	case "day":
		if user_id == 0 {
			execSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id where r.created_at like '%s%%' group by r.user_id,u.name", currentDay)
			db.Raw(execSql).Scan(&showDatas)
		} else {
			userexecSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id where r.created_at like '%s%%' and r.user_id=%d group by r.user_id,u.name", currentDay, user_id)
			db.Raw(userexecSql).Scan(&showDatas)
		}
	case "week":

		startOfWeek := carbon.NewDateTime(carbon.Now()).StartOfWeek().Format("Y-m-d H:i:s")
		endOfWeek := carbon.NewDateTime(carbon.Now()).EndOfWeek().Format("Y-m-d H:i:s")
		if user_id == 0 {
			execSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id where r.created_at between '%s' and '%s' group by r.user_id,u.name", startOfWeek, endOfWeek)
			db.Raw(execSql).Scan(&showDatas)
		} else {
			userexecSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id where r.user_id=%d and r.created_at between '%s' and '%s' group by r.user_id,u.name", user_id, startOfWeek, endOfWeek)
			db.Raw(userexecSql).Scan(&showDatas)
		}
	case "month":
		if user_id == 0 {

			startOfMonth := carbon.NewDateTime(carbon.Now()).StartOfMonth().Format("Y-m-d H:i:s")
			endOfMonth := carbon.NewDateTime(carbon.Now()).EndOfMonth().Format("Y-m-d H:i:s")
			execSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id   where r.created_at between '%s' and '%s' group by r.user_id,u.name", startOfMonth, endOfMonth)
			db.Raw(execSql).Scan(&showDatas)
		} else {
			startOfMonth := carbon.NewDateTime(carbon.Now()).StartOfMonth().Format("Y-m-d H:i:s")
			endOfMonth := carbon.NewDateTime(carbon.Now()).EndOfMonth().Format("Y-m-d H:i:s")
			userexecSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id   where u.id=%d and r.created_at between '%s' and '%s' group by r.user_id,u.name", user_id, startOfMonth, endOfMonth)
			db.Raw(userexecSql).Scan(&showDatas)
		}
	case "year":
		if user_id == 0 {
			startOfYear := carbon.NewDateTime(carbon.Now()).StartOfYear().Format("Y-m-d H:i:s")
			endOfYear := carbon.NewDateTime(carbon.Now()).EndOfYear().Format("Y-m-d H:i:s")
			execSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id where r.created_at between '%s' and '%s' group by r.user_id,u.name ", startOfYear, endOfYear)
			db.Raw(execSql).Scan(&showDatas)
		} else {
			startOfYear := carbon.NewDateTime(carbon.Now()).StartOfYear().Format("Y-m-d H:i:s")
			endOfYear := carbon.NewDateTime(carbon.Now()).EndOfYear().Format("Y-m-d H:i:s")
			userexecSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id  where u.id=%d and r.created_at between '%s' and '%s' group by r.user_id,u.name", user_id, startOfYear, endOfYear)
			db.Raw(userexecSql).Scan(&showDatas)
		}
	case "custom":
		if user_id == 0 {
			execSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id where r.created_at between '%s' and '%s' group by r.user_id,u.name ", start_at, end_at)
			db.Raw(execSql).Scan(&showDatas)
		} else {
			userexecSql := fmt.Sprintf("select r.user_id,u.name,sum(r.score) as data from records r left join  users u ON r.user_id = u.id where  r.user_id=%d and r.created_at between '%s' and '%s' group by r.user_id,u.name ", user_id, start_at, end_at)
			db.Raw(userexecSql).Scan(&showDatas)
		}
	}
	return httpfacade.NewResult(ctx).Success("", showDatas)
}
